﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lib.CommonForm;

namespace AUI
{
    public partial class frmDrugTypesTree : FormBase
    {
        public frmDrugTypesTree()
        {
            InitializeComponent();

            _listRole = tbl_RoleRight_Controller.Instance().GetInfo("LoaiThuoc");



            DrugTypes_LoadTree();
        }
        public override void Show_Form()
        {
            this.Show();
        }
        void DrugTypes_LoadTree()
        {
            ctrlTreeView1.HideSelection = false;
            ctrlTreeView1.Nodes.Clear();
            //
            TreeNode rNode;
            rNode = ctrlTreeView1.Nodes.Add("Root", "Loại thuốc", 0, 0);
            rNode.Tag = 0;
            //
            DrugTypes_AddChildNode(rNode);
            //
            ctrlTreeView1.ExpandAll();
        }
        void DrugTypes_AddChildNode(TreeNode parentNode)
        {
            DataTable dt = tbl_DrugTypes_Controller.Instance().GetByParentId(Globals.ToInt32(parentNode.Tag));

            if (dt.Rows.Count > 0)
            {
                TreeNode node;
                foreach (DataRow dr in dt.Rows)
                {
                    node = parentNode.Nodes.Add(dr["DrugTypeId"].ToString(), dr["DrugTypeCode"].ToString() + ": " + dr["DrugTypeName"].ToString(), 1, 1);
                    node.Tag = Globals.ToInt32(dr["DrugTypeId"]);

                    DrugTypes_AddChildNode(node);
                }
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            DrugTypes_LoadTree();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (FormGlobals.Message_Confirm(UIMessage.QUESTION_DELETE))
            {
                if (tbl_DrugTypes_Controller.Instance().Delete(_DrugTypeId))
                {

                    ctrlTreeView1.Nodes.Remove(ctrlTreeView1.SelectedNode);

                    FormGlobals.Message_InTaskbar(UIMessage.DETETE_COMPLETE);
                }
                else
                    FormGlobals.Message_Error(new Exception(UIMessage.DETETE_ERROR));
            }

        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            frmDrugTypeTreeInfo f = new frmDrugTypeTreeInfo(0, _DrugTypeId);
            if (f.ShowDialog(this) == DialogResult.OK)
            {
                ctrlTreeView1.SelectedNode.Text = f.DrugTypeCode + ": " + f.DrugTypeName;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {

            frmDrugTypeTreeInfo f = new frmDrugTypeTreeInfo(_DrugTypeId, 0);
            if (f.ShowDialog(this) == DialogResult.OK)
            {
                ctrlTreeView1.SelectedNode.Nodes.Add(f.DrugTypeId.ToString(), f.DrugTypeCode + ": " + f.DrugTypeName, 1, 1).Tag = f.DrugTypeId;
                ctrlTreeView1.ExpandAll();
            }
        }

        int _DrugTypeId = 0;
        private void ctrlTreeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            _DrugTypeId = Globals.ToInt32(e.Node.Tag);
        }

        private void ctrlTreeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            //For right click to select a node in a treeview control
            if (e.Button == MouseButtons.Right)
                ctrlTreeView1.SelectedNode = e.Node;
        }

        private void frmDrugTypesTree_Load(object sender, EventArgs e)
        {
            Control_EnableByRole(this);
            mnuAdd.Enabled = btnAdd.Enabled;
            mnuEdit.Enabled = btnEdit.Enabled;
            mnuDelete.Enabled = btnDelete.Enabled;
        }

        private void btnExcel_Click(object sender, EventArgs e)
        {
            if (!FormGlobals.Message_Confirm("Bạn có chắc chắn " + btnExcel.Text + "?")) return;

            try
            {
                DataTable dtExcel = tbl_DrugTypes_Controller.Instance().GetAll();
                ReadExcelFile.MSExcelExport(dtExcel, this.Text);
            }
            catch (Exception ex)
            {
                FormGlobals.Message_Warning(ex.Message);
                return;
            }
        }

    }
}
